package com.qf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.dao.CartDao;
import com.qf.entity.Cart;
import com.qf.entity.Goods;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 购物车表(Cart)表服务实现类
 *
 * @author makejava
 * @since 2023-04-07 14:36:03
 */
@Service("cartService")
public class CartServiceImpl extends ServiceImpl<CartDao, Cart> implements CartService {
    @Autowired
    private GoodsService goodsService;
    @Override
    public List<Cart> quearyByUid(int uid) {
        List<Cart> carts = this.query().eq("uid", uid).list();
        return carts;
    }

    @Override
    @Transactional
    public int updateByNumber(int gid, int number) {
        //用商品gid查出单价,然后改变总价
        Goods goods = goodsService.queryPrice(gid);
        //改变数量 "quantity=quantity"+number
        //this.update().setSql("quantity=quantity"+number).eq("gid", gid);
        getBaseMapper().updateByNumber(gid, number);
        //查询出改变后的数量
        Cart cart = this.query().eq("gid", gid).one();
        //改变总价
        Double price = goods.getPrice();
        int quantity = cart.getQuantity();
        Double a=price*quantity;
       getBaseMapper().updateByQuantity(a,gid);
        return 1;

    }
}

